package patrol.mobile.com.mobilepatrol.persenter;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.MediaType;
import patrol.mobile.com.mobilepatrol.utils.Constants;
import patrol.mobile.com.mobilepatrol.utils.SPUtils;
import patrol.mobile.com.mobilepatrol.view.BridgeUpView;

/**
 * Created by apple on 17/9/30.
 */

public class QHUpPer {

    private BridgeUpView mView;
    private String json;

    public QHUpPer(BridgeUpView mView, String json) {
        this.mView = mView;
        this.json = json;
    }

    public void up2(String url) {
        OkHttpUtils
                .postString()
                .url(SPUtils.get(Constants.current_ser,"") + url)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(json)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        mView.onError("上传失败");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            JSONObject obj = new JSONObject(response);
                            int state = obj.optInt("state");
                            if (state ==200){
                                mView.onSuccess("上传成功");
                            }else{
                                mView.onError("上传失败");
                            }
                        } catch (JSONException e) {

                        }

                    }
                });

    }
}
